package btd.game.dlgs;

import btd.game.ConstData;
import btd.game.DBTools;
import btd.game.activities.PBoardAct;
import btd.game.activities.R;
import btd.game.activities.SdeskAct;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ConstTextListDlg extends Dialog {
	private Activity owner = null;
	private ListView lvConstTextList = null;
	private ArrayAdapter<String> arrayAdapter = null;
	private boolean isSdeskAct = false;
	private boolean isFDlg = false;
	private DBTools dbTools = null;
	public ConstTextListDlg(Context context, boolean isSdeskAct, boolean isFDlg) {
		super(context);
		this.setTitle(R.string.selectconsttext);
		this.isSdeskAct = isSdeskAct;
		this.isFDlg = isFDlg;
		if(isSdeskAct) {
		    owner = (SdeskAct) context;
		} else {
			owner = (PBoardAct) context;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.consttextlistdlg);
		this.setTitle(R.string.selectconsttext);
		
		lvConstTextList = (ListView) findViewById(R.id.ListViewConstTextList);
		lvConstTextList.setOnItemClickListener(itemCliListener);
		arrayAdapter = new ArrayAdapter<String>(owner, android.R.layout.simple_list_item_1);
		if(isFDlg) {
			for(String info : owner.getResources().getStringArray(R.array.consttextlist)) {
				arrayAdapter.add(info);
			}
		} else {
			dbTools = new DBTools(owner);
			String[] infos = dbTools.getConstTexts().split("_");if(infos.length == 5) {
				arrayAdapter.add(infos[0]);
				arrayAdapter.add(infos[1]);
				arrayAdapter.add(infos[2]);
				arrayAdapter.add(infos[3]);
				arrayAdapter.add(infos[4]);
			}
		}
		arrayAdapter.add(this.owner.getString(R.string.back));
		lvConstTextList.setAdapter(arrayAdapter);
	}

	private OnItemClickListener itemCliListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int index, long id) {
			if(index < lvConstTextList.getCount() - 1) {
				String info = ((String) lvConstTextList.getItemAtPosition(index));
				if(isSdeskAct) {
					((SdeskAct) owner).setChatInfo(info);
				} else {
					((PBoardAct) owner).setChatInfo(info);
				}
			}
			ConstTextListDlg.this.cancel();
		}
	};
}
